﻿//***********************************************************************
// Assembly         : NMortgage.UI
// Author           : Mike
// Created          : 04-29-2012
//
// Last Modified By : Mike
// Last Modified On : 04-27-2012
// Description      : 
//
// Copyright        : (c) . All rights reserved.
//***********************************************************************
using System.Globalization;
namespace NMortgage.UI.Model
{
    using System.Collections.Generic;

    public sealed class InterestParameter : ABaseValidatedType<InterestParameter, InterestParameterValidator>, IInterestRate
    {
        #region Fields

        private const int MONTHS_IN_YEAR = 12;

        private int months;
        private Dictionary<DistributionType, IDistribution> rates;
        int years;

        #endregion Fields

        #region Constructors

        public InterestParameter()
            : this(DistributionType.FixedRate)
        {
        }

        public InterestParameter(DistributionType rateType)
        {
            DistributionType = rateType;
            rates = InitRates();
            Years = 20;
        }

        #endregion Constructors

        #region Properties

        public string Description
        {
            get
            {
                return string.Format(CultureInfo.CurrentCulture, Properties.Resources.InterestParameterDescriptionFormat,
                      Distribution.Description, TotalDurationInMonths,
                      Properties.Resources.MonthsLabel);
            }
        }

        public IDistribution Distribution
        {
            get
            {
                return rates[DistributionType];
            }
        }

        public DistributionType DistributionType
        {
            get;
            set;
        }

        public int Months
        {
            get
            {
                return months;
            }
            set
            {
                months = value;
                NotifyOfPropertyChange(() => Years);
                NotifyOfPropertyChange(() => Months);
                NotifyOfPropertyChange(() => Description);
            }
        }

        public int TotalDurationInMonths
        {
            get { return (Years * MONTHS_IN_YEAR) + Months; }
        }

        public int Years
        {
            get
            {
                return years;
            }
            set
            {
                years = value;
                NotifyOfPropertyChange(() => Years);
                NotifyOfPropertyChange(() => Months);
                NotifyOfPropertyChange(() => Description);
            }
        }

        #endregion Properties

        #region Methods

        private Dictionary<DistributionType, IDistribution> InitRates()
        {
            Dictionary<DistributionType, IDistribution> d = new Dictionary<DistributionType, IDistribution>();
            d.Add(Model.DistributionType.FixedRate, new FixedRate());
            d.Add(Model.DistributionType.BetaDistribution, new BetaDistribution());
            d.Add(Model.DistributionType.NormalDistribution, new NormalDistribution());
            d.Add(Model.DistributionType.UniformDistribution, new UniformDistribution());

            return d;
        }

        #endregion Methods
    }
}